package com.wenheng.customer;

import com.rabbitmq.client.Channel;
import com.wenheng.Business.SeckillingBusiness;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Auther: 文恒
 * @Date: 2021/3/25 - 03 - 25 -14:39
 * @Description:com.wenheng.customer
 * @version:1.0
 */
@Component
public class QueueListener {
    @Autowired
    private SeckillingBusiness seckillingBusiness;

    @RabbitListener(queuesToDeclare = @Queue("myqueue"), ackMode = "MANUAL")  //创建了一个消费者
    public void displayMail(Channel channel, String ids, Message message) throws Exception {
        channel.basicQos(1);
        System.out.println("消费者1收到消息：id:" + ids);
        String[] numList = ids.split(",");
        Integer id = null;
        Integer activityId = null;
        Integer NumSeckill = null;
        String token = null;
        int i = 0;
        for (String s : numList) {
            if (i == 0) {
                id = new Integer(s);
            } else if (i == 1) {
                activityId = new Integer(s);
            } else if (i == 2) {
                NumSeckill = new Integer(s);
            } else {
                token = s;
            }
            i++;
        }
        if (seckillingBusiness.ExecutiveBusiness(id, activityId, NumSeckill,token)) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); //确认收到消息
        } else {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); // 拒绝消息
        }
    }


    @RabbitListener(queuesToDeclare = @Queue("myqueue"), ackMode = "MANUAL")  //创建了一个消费者
    public void displayMail2(Channel channel, String ids, Message message) throws Exception {
        channel.basicQos(1);
        System.out.println("消费者2收到消息：id:" + ids);
        String[] numList = ids.split(",");
        Integer id = null;
        Integer activityId = null;
        Integer NumSeckill = null;
        String token = null;
        int i = 0;
        for (String s : numList) {
            if (i == 0) {
                id = new Integer(s);
            } else if (i == 1) {
                activityId = new Integer(s);
            } else if (i == 2) {
                NumSeckill = new Integer(s);
            } else {
                token = s;
            }
            i++;
        }
        if (seckillingBusiness.ExecutiveBusiness(id, activityId, NumSeckill,token)) {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); //确认收到消息
        } else {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); // 拒绝消息
        }
    }
}
